package com.pro.controller;

import com.pro.pojo.JsApplyRecord;
import com.pro.pojo.JsBeforeUser;
import com.pro.pojo.JsCourse;
import com.pro.pojo.JsonData;
import com.pro.service.indexWelcomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/indexWelcome")
public class indexWelcomeController {

    @Autowired
    private indexWelcomeService iws;

    //视频上传数
    @RequestMapping("/getJsCourseCount")
    @ResponseBody
    public Integer getJsCourseCount(){
        return iws.getJsCourseCount();
    }

    //视频收藏、浏览、点赞数
    @RequestMapping("/getJsCourseCollect")
    @ResponseBody
    public JsonData getJsCourseCollect(){
        List<JsCourse> jsCourseCollect = iws.getJsCourseCollect();
        return jsCourseCollect!=null? JsonData.buildSuccess(jsCourseCollect):JsonData.buildError("服务器出错了");
    }

    //课程上架数量
    @RequestMapping("/getCourseCount")
    @ResponseBody
    public Integer getCourseCount(){
        return iws.getCourseCount();
    }

    //课程上架数量
    @RequestMapping("/getProductCount")
    @ResponseBody
    public Integer getProductCount(){
        return iws.getProductCount();
    }

    //用户数量
    @RequestMapping("/getUserCount")
    @ResponseBody
    public Integer getUserCount(){
        return iws.getUserCount();
    }

    //用户新增数
    @RequestMapping("/getJsBeforeUserCount")
    @ResponseBody
    public JsonData getJsBeforeUserCount(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        //获取当前时间
        String format = sdf.format(new Date());
        String format1 = sdf1.format(new Date());
        //获取昨天时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE,-1);
        String date2= sdf.format(calendar.getTime());

        int today=0;
        int yestday=0;
        int mouth=0;

        List<JsBeforeUser> jsBeforeUserCount1 = iws.getJsBeforeUserCount();
        for (JsBeforeUser jsBeforeUser : jsBeforeUserCount1) {
            if(jsBeforeUser.getCreateDate().indexOf(format)!=-1){
                today++;
            }else if(jsBeforeUser.getCreateDate().indexOf(date2)!=-1){
                yestday++;
            }
        }

        for (JsBeforeUser jsBeforeUser : jsBeforeUserCount1) {
            if(jsBeforeUser.getCreateDate().indexOf(format1)!=-1){
                mouth++;
            }
        }

        List list=new ArrayList();
        list.add(today);
        list.add(yestday);
        list.add(mouth);

        return list!=null? JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //本月课程新增比例
    @RequestMapping("/getJsCourse")
    @ResponseBody
    public JsonData getJsCourse(){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        //获取当前时间
        String format1 = sdf1.format(new Date());

        int mouth=0;

        List<JsCourse> jsCourse = iws.getJsCourse();

        for (JsCourse jsCourse1 : jsCourse) {
            if(jsCourse1.getCreateDate().indexOf(format1)!=-1){
                mouth++;
            }
        }

        List list=new ArrayList();
        list.add(mouth);

        return list!=null? JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //上月课程新增比例
    @RequestMapping("/getLastJsCourse")
    @ResponseBody
    public JsonData getLastJsCourse(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //获取当前时间
        String format = sdf.format(new Date());
        //获取上个月时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.MONTH,-1);
        String date2= sdf.format(calendar.getTime());

        int mouth=0;

        List<JsCourse> jsCourse = iws.getJsCourse();
        for (JsCourse jsCourse1 : jsCourse) {
            if(jsCourse1.getCreateDate().indexOf(date2)!=-1){
                mouth++;
            }
        }

        List list=new ArrayList();
        list.add(mouth);

        return list!=null? JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //本月申请新增比例
    @RequestMapping("/getJsApplyRecord")
    @ResponseBody
    public JsonData getJsApplyRecord(){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        //获取当前时间
        String format1 = sdf1.format(new Date());

        int mouth=0;

        List<JsApplyRecord> jsApplyRecord = iws.getJsApplyRecord();
        for (JsApplyRecord jsApplyRecord1 : jsApplyRecord) {
            if(jsApplyRecord1.getApplyDate().indexOf(format1)!=-1){
                mouth++;
            }
        }

        List list=new ArrayList();
        list.add(mouth);

        return list!=null? JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //上月申请新增比例
    @RequestMapping("/getLastJsApplyRecord")
    @ResponseBody
    public JsonData getLastJsApplyRecord(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //获取当前时间
        String format = sdf.format(new Date());
        //获取上个月时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.MONTH,-1);
        String date2= sdf.format(calendar.getTime());

        int mouth=0;

        List<JsApplyRecord> jsApplyRecord = iws.getJsApplyRecord();
        for (JsApplyRecord jsApplyRecord1 : jsApplyRecord) {
            if(jsApplyRecord1.getApplyDate().indexOf(date2)!=-1){
                mouth++;
            }
        }

        List list=new ArrayList();
        list.add(mouth);

        return list!=null? JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //课程今日新增图表
    @RequestMapping("/getTodayJsBeforeUserCount")
    @ResponseBody
    public JsonData getTodayJsBeforeUserCount() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间
        String format = sdf.format(new Date());

        int three=0;
        int six=0;
        int nine=0;
        int twelve=0;
        int fifteen=0;
        int eighteen=0;
        int twentyOne=0;

        List<JsCourse> jsCourse = iws.getJsCourse();
        List list=new ArrayList();
        //拿到每日的时间
        String year=format;
        //定义0-3点之间的时间
        String time0=year+" 00:00:00";
        String time1=year+" 03:00:00";
        String time2=year+" 06:00:00";
        String time3=year+" 09:00:00";
        String time4=year+" 12:00:00";
        String time5=year+" 15:00:00";
        String time6=year+" 18:00:00";
        String time7=year+" 21:00:00";
        for (JsCourse jsCourse1 : jsCourse) {
            //判断课程创建时间是否是今日时间
            if(jsCourse1.getCreateDate().indexOf(format)!=-1){
                //拿到0和3点的毫秒值并判断课程创建日期的毫秒值是否在这个区间内
                if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time1).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time0).getTime()){
                    three++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time2).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time1).getTime()){
                    six++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time3).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time2).getTime()){
                    nine++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time4).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time3).getTime()){
                    twelve++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time5).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time4).getTime()){
                    fifteen++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time6).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time5).getTime()){
                    eighteen++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time7).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time6).getTime()){
                    twentyOne++;
                }
            }
        }

        list.add(three);
        list.add(six);
        list.add(nine);
        list.add(twelve);
        list.add(fifteen);
        list.add(eighteen);
        list.add(twentyOne);

        return list!=null? JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //课程本周新增图表
    @RequestMapping("/getWeekJsBeforeUserCount")
    @ResponseBody
    public JsonData getWeekJsBeforeUserCount() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        // 今天是一周中的第几天
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK );

        if (c.getFirstDayOfWeek() == Calendar.SUNDAY) {
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
        // 计算一周开始的日期
        c.add(Calendar.DAY_OF_MONTH, -dayOfWeek);

        List<Long> list=new ArrayList();
        //获取这周每天的毫秒值并添加至list集合内
        for (int i=1;i<=7;i++) {
            c.add(Calendar.DAY_OF_MONTH, 1);
            list.add(sdf.parse(sdf.format(c.getTime())+" 00:00:00").getTime());
        }

        int monday=0;
        int tuesday=0;
        int wednesday=0;
        int thursday=0;
        int friday=0;
        int saturday=0;
        int sunday=0;

        //获取所有课程的上架时间
        List<JsCourse> jsCourse = iws.getJsCourse();
        //遍历所有课程的上架时间
        for (JsCourse course : jsCourse) {
            //遍历集合内所存放这周每天的毫秒值
            for(int i=0;i<list.size();i++){
                //判断当前课程上架时间的毫秒值是否在周一与周二的毫秒值之间 以此类推
                if(sdf1.parse(course.getCreateDate()).getTime()>=list.get(0) && sdf1.parse(course.getCreateDate()).getTime()<list.get(1)){
                    monday++;
                    break;
                }else if(sdf1.parse(course.getCreateDate()).getTime()>=list.get(1) && sdf1.parse(course.getCreateDate()).getTime()<list.get(2)){
                    tuesday++;
                    break;
                }else if(sdf1.parse(course.getCreateDate()).getTime()>=list.get(2) && sdf1.parse(course.getCreateDate()).getTime()<list.get(3)){
                    wednesday++;
                    break;
                }else if(sdf1.parse(course.getCreateDate()).getTime()>=list.get(3) && sdf1.parse(course.getCreateDate()).getTime()<list.get(4)){
                    thursday++;
                    break;
                }else if(sdf1.parse(course.getCreateDate()).getTime()>=list.get(4) && sdf1.parse(course.getCreateDate()).getTime()<list.get(5)){
                    friday++;
                    break;
                }else if(sdf1.parse(course.getCreateDate()).getTime()>=list.get(5) && sdf1.parse(course.getCreateDate()).getTime()<list.get(6)){
                    saturday++;
                    break;
                }else if(sdf1.parse(course.getCreateDate()).getTime()>=list.get(6) && sdf1.parse(course.getCreateDate()).getTime()<list.get(6)+86400000){
                    sunday++;
                    break;
                }
            }
        }

        List list1=new ArrayList();
        list1.add(monday);
        list1.add(tuesday);
        list1.add(wednesday);
        list1.add(thursday);
        list1.add(friday);
        list1.add(saturday);
        list1.add(sunday);

        return list1!=null? JsonData.buildSuccess(list1):JsonData.buildError("服务器出错了");
    }


    //课程本月新增图表
    @RequestMapping("/getMonthJsBeforeUserCount")
    @ResponseBody
    public JsonData getMonthJsBeforeUserCount() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间
        String format = sdf.format(new Date());

        int three=0;
        int six=0;
        int nine=0;
        int twelve=0;
        int fifteen=0;
        int eighteen=0;

        List<JsCourse> jsCourse = iws.getJsCourse();
        List list=new ArrayList();
        String year=format;
        String time0=year+"-00 00:00:00";
        String time1=year+"-07 00:00:00";
        String time2=year+"-13 00:00:00";
        String time3=year+"-19 00:00:00";
        String time4=year+"-25 00:00:00";
        String time5=year+"-30 00:00:00";
        String time6=year+"-31 24:00:00";
        for (JsCourse jsCourse1 : jsCourse) {
            //判断课程创建时间是否是本月时间
            if(jsCourse1.getCreateDate().indexOf(format)!=-1){
                if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time1).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time0).getTime()){
                    three++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time2).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time1).getTime()){
                    six++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time3).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time2).getTime()){
                    nine++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time4).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time3).getTime()){
                    twelve++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time5).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time4).getTime()){
                    fifteen++;
                }else if(sdf1.parse(jsCourse1.getCreateDate()).getTime() <= sdf1.parse(time6).getTime() && sdf1.parse(jsCourse1.getCreateDate()).getTime() > sdf1.parse(time5).getTime()){
                    eighteen++;
                }
            }
        }

        list.add(three);
        list.add(six);
        list.add(nine);
        list.add(twelve);
        list.add(fifteen);
        list.add(eighteen);

        return list!=null? JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //商品待审核数量
    @RequestMapping("/getJsProductRecord")
    @ResponseBody
    public Integer getJsProductRecord(){
        return iws.getJsProductRecord();
    }

    //视频待审核数量
    @RequestMapping("/getJsCourseRecord")
    @ResponseBody
    public Integer getJsCourseRecord(){
        return iws.getJsCourseRecord();
    }

    //微圈待审核数量
    @RequestMapping("/getJsMicrocircleRecord")
    @ResponseBody
    public Integer getJsMicrocircleRecord(){
        return iws.getJsMicrocircleRecord();
    }


}
